package aliyun.cx.admin.controller;

import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import aliyun.cx.admin.dto.response.ApiResponse;
import aliyun.cx.admin.dto.response.dashboard.DashboardStatsResponse;
import aliyun.cx.admin.entity.User;
import aliyun.cx.admin.entity.Article;
import aliyun.cx.admin.entity.Comment;
import aliyun.cx.admin.mapper.UserMapper;
import aliyun.cx.admin.mapper.ArticleMapper;
import aliyun.cx.admin.mapper.CommentMapper;
import aliyun.cx.admin.mapper.AlbumMapper;
import aliyun.cx.admin.mapper.CategoryMapper;
import aliyun.cx.admin.mapper.TagMapper;
import aliyun.cx.admin.mapper.PhotoMapper;

@RestController
@RequestMapping("/api/dashboard")
@CrossOrigin(origins = "*", maxAge = 3600)
public class DashboardController {

    private final UserMapper userMapper;
    private final ArticleMapper articleMapper;
    private final CommentMapper commentMapper;
    private final AlbumMapper albumMapper;
    private final CategoryMapper categoryMapper;
    private final TagMapper tagMapper;
    private final PhotoMapper photoMapper;
    
    public DashboardController(UserMapper userMapper, 
                             ArticleMapper articleMapper, 
                             CommentMapper commentMapper, 
                             AlbumMapper albumMapper, 
                             CategoryMapper categoryMapper, 
                             TagMapper tagMapper, 
                             PhotoMapper photoMapper) {
        this.userMapper = userMapper;
        this.articleMapper = articleMapper;
        this.commentMapper = commentMapper;
        this.albumMapper = albumMapper;
        this.categoryMapper = categoryMapper;
        this.tagMapper = tagMapper;
        this.photoMapper = photoMapper;
    }

    /**
     * 获取仪表盘统计数据
     */
    @GetMapping("/stats")
    public ApiResponse<DashboardStatsResponse> getDashboardStats() {
        try {
            // 统计用户数量
            long userCount = userMapper.selectCount(null);
            
            // 统计文章数量
            long articleCount = articleMapper.selectCount(null);
            
            // 统计评论数量
            long commentCount = commentMapper.selectCount(null);
            
            // 统计相册数量
            long albumCount = albumMapper.selectCount(null);
            
            // 统计分类数量
            long categoryCount = categoryMapper.selectCount(null);
            
            // 统计标签数量
            long tagCount = tagMapper.selectCount(null);
            
            // 统计照片数量
            long photoCount = photoMapper.selectCount(null);
            
            // 统计已发布的文章数量
            QueryWrapper<Article> publishedWrapper = new QueryWrapper<>();
            publishedWrapper.eq("is_published", 1);
            long publishedArticleCount = articleMapper.selectCount(publishedWrapper);
            
            // 统计今日新增用户
            QueryWrapper<User> todayUserWrapper = new QueryWrapper<>();
            todayUserWrapper.apply("DATE(create_time) = CURDATE()");
            long todayUserCount = userMapper.selectCount(todayUserWrapper);
            
            // 统计今日新增文章
            QueryWrapper<Article> todayArticleWrapper = new QueryWrapper<>();
            todayArticleWrapper.apply("DATE(create_time) = CURDATE()");
            long todayArticleCount = articleMapper.selectCount(todayArticleWrapper);
            
            // 统计今日新增评论
            QueryWrapper<Comment> todayCommentWrapper = new QueryWrapper<>();
            todayCommentWrapper.apply("DATE(create_time) = CURDATE()");
            long todayCommentCount = commentMapper.selectCount(todayCommentWrapper);
            
            DashboardStatsResponse response = new DashboardStatsResponse();
            response.setUserCount(userCount);
            response.setArticleCount(articleCount);
            response.setCommentCount(commentCount);
            response.setAlbumCount(albumCount);
            response.setCategoryCount(categoryCount);
            response.setTagCount(tagCount);
            response.setPhotoCount(photoCount);
            response.setPublishedArticleCount(publishedArticleCount);
            response.setTodayUserCount(todayUserCount);
            response.setTodayArticleCount(todayArticleCount);
            response.setTodayCommentCount(todayCommentCount);
            
            return ApiResponse.success("获取统计数据成功", response);
            
        } catch (Exception e) {
            return ApiResponse.error(500, "获取统计数据失败：" + e.getMessage());
        }
    }
}
